package dao;

import model.Supplier;
import model.Trade;
import model.TradeType;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Dao_trade extends Dao{
    private static List tradeTypes;
    private static List tradeType;

    public static Vector<String> getTradeType()
    {
        List list = Dao_type.selectAll();
        Vector<String> TradeTypes = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            TradeType tradeType = (TradeType) list.get(i);
            TradeTypes.add(tradeType.getType_name());
        }

        return TradeTypes;
    }


    //通过商品类型查询id
    public static String getTypeID_Name(String tradeName)
    {
        if(tradeTypes == null) tradeTypes = Dao_type.selectAll();

        for(int i = 0; i < tradeTypes.size(); i++)
        {
            TradeType tradeType = (TradeType) tradeTypes.get(i);
            if(tradeType.getType_name().equals(tradeName))  return tradeType.getType_id();
        }

        return null;
    }

    //通过商品类型id查询商品类型
    public static String getTradeName_ID(String tradeID)
    {
        if(tradeTypes == null) tradeTypes = Dao_type.selectAll();

        for(int i = 0; i < tradeTypes.size(); i++)
        {
            TradeType bookType = (TradeType) tradeTypes.get(i);
            if(tradeID.equals( bookType.getType_id()))
                return bookType.getType_name();
        }

        return null;
    }

    //添加商品
    public static int insert(String trade_id, String typeId, String tradeName, String quantity,Double price, Double discount)
    {
        int i = 0;
        try {
            String sql="insert into commodity(c_no,cs_no,c_name,number,discount,price) values('"+trade_id+"','"+typeId+"','"+tradeName+"',"+quantity+",'"+discount+"',"+price+")";
            i = Dao.exeUpdate(sql);


        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //通过商品类型查看商品
    public static List fuzzySelect(String typeID){
        List list = new ArrayList();
        String sql = "select * from commodity where cs_no = '" + typeID + "'";

        ResultSet re = Dao.executeQuery(sql);
        try {
            while (re.next())
            {
                Trade trade = new Trade();

                trade.setTradeName(re.getString("c_name"));
                trade.setTradeID(re.getString("c_no"));
                trade.setNumber(re.getInt("number"));
                trade.setPrice(re.getDouble("price"));
                trade.setDiscount(re.getDouble("discount"));
                trade.setTypeID(re.getString("cs_no"));

                list.add(trade);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //商品编号查询
    public static List fuzzySelect_1(String tradeID){
        List list = new ArrayList();
        String sql = "select * from commodity where c_no like '%" + tradeID + "%'";

        ResultSet re = Dao.executeQuery(sql);
        try {
            while (re.next())
            {
                Trade trade = new Trade();

                trade.setTradeName(re.getString("c_name"));
                trade.setTradeID(re.getString("c_no"));
                trade.setNumber(re.getInt("number"));
                trade.setPrice(re.getDouble("price"));
                trade.setDiscount(re.getDouble("discount"));
                trade.setTypeID(re.getString("cs_no"));

                list.add(trade);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }


    //商品名称查询
    public static List fuzzySelect_2(String tradeName){
        List list = new ArrayList();
        String sql = "select * from commodity where c_name like '%" + tradeName + "%'";

        ResultSet re = Dao.executeQuery(sql);
        try {
            while (re.next())
            {
                Trade trade = new Trade();

                trade.setTradeName(re.getString("c_name"));
                trade.setTradeID(re.getString("c_no"));
                trade.setNumber(re.getInt("number"));
                trade.setPrice(re.getDouble("price"));
                trade.setDiscount(re.getDouble("discount"));
                trade.setTypeID(re.getString("cs_no"));

                list.add(trade);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //获取所有商品
    public static List fuzzySelectAll(){
        List list = new ArrayList();
        String sql = "select * from commodity";

        ResultSet re = Dao.executeQuery(sql);
        try {
            while (re.next())
            {
                Trade trade = new Trade();

                trade.setTradeName(re.getString("c_name"));
                trade.setTradeID(re.getString("c_no"));
                trade.setNumber(re.getInt("number"));
                trade.setPrice(re.getDouble("price"));
                trade.setDiscount(re.getDouble("discount"));
                trade.setTypeID(re.getString("cs_no"));

                list.add(trade);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //删除商品
    public static int deleteTrade(String tradeID) {
        int i = 0;
        try {
            String sql = "delete from  commodity where c_no = '" + tradeID + "'";
            i = Dao.exeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Dao.close();

        return i;

    }


    //修改商品信息
    public static int update(String trade_id, String typeId, String tradeName, String quantity,Double price, Double discount)
    {
        int i = 0;
        try {
            String sql="update commodity set c_no = '"+trade_id+"',cs_no = '"+typeId+"',c_name = '"+tradeName+"',number = "+quantity+",discount = "+discount+",price = "+price+" where c_no = '" + trade_id +"'";
            i = Dao.exeUpdate(sql);
        }

        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //获取商品名
    public static Vector<String> getTrade()
    {
        List list = Dao_trade.fuzzySelectAll();
        Vector<String> WareID = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            Trade trade = (Trade) list.get(i);
            WareID.add(trade.getTradeName());
        }

        return WareID;
    }

    //获取所有商品ID
    public static Vector<String> getTradeID()
    {
        List list = Dao_trade.fuzzySelectAll();
        Vector<String> WareID = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            Trade trade = (Trade) list.get(i);
            WareID.add(trade.getTradeID());
        }

        return WareID;
    }


    //通过商品名称获取商品类型ID
    public static String getTradeName_typeID(String tradeName) {
        if(tradeType == null)
            tradeType = Dao_trade.fuzzySelectAll();

        for (int i = 0; i < tradeType.size(); i++) {

            Trade trade = (Trade) tradeType.get(i);

            if (tradeName.equals(trade.getTradeName()))
            {
                return trade.getTypeID();
            }

        }

        return null;
    }

    //通过商品名称获取商品id
    public static String getTradeName_tradeID(String tradeName) {
        if(tradeType == null)
            tradeType = Dao_trade.fuzzySelectAll();

        for (int i = 0; i < tradeType.size(); i++) {

            Trade trade = (Trade) tradeType.get(i);

            if (tradeName.equals(trade.getTradeName()))

                return trade.getTradeID();
        }

        return null;
    }

    //通过商品名称获取商品单价
    public static double getTradeName_price(String tradeName) {
        if(tradeType == null)
            tradeType = Dao_trade.fuzzySelectAll();

        for (int i = 0; i < tradeType.size(); i++) {
            Trade trade = (Trade) tradeType.get(i);

            if (tradeName.equals(trade.getTradeName()))

                return trade.getPrice();
        }

        return 0;
    }

    //通过商品名称获取折扣
    public static double getTradeName_discount(String tradeName) {
        if(tradeType == null)
            tradeType = Dao_trade.fuzzySelectAll();

        for (int i = 0; i < tradeType.size(); i++) {

            Trade trade = (Trade) tradeType.get(i);

            if (tradeName.equals(trade.getTradeName()))

                return trade.getDiscount();
        }

        return 0;
    }

    //通过商品名称获取库存数量
    public static int getTradeName_number(String tradeName) {
        if(tradeType == null)
            tradeType = Dao_trade.fuzzySelectAll();

        for (int i = 0; i < tradeType.size(); i++) {

            Trade trade = (Trade) tradeType.get(i);

            if (tradeName.equals(trade.getTradeName()))

                return trade.getNumber();
        }

        return 0;
    }

}
